﻿using System;
using Terraria.ModLoader.Exceptions;

namespace Terraria.ModLoader
{
	// Token: 0x020001F1 RID: 497
	public class ModRecipe : Recipe
	{
		// Token: 0x17000198 RID: 408
		// (get) Token: 0x0600157E RID: 5502 RVA: 0x0001080F File Offset: 0x0000EA0F
		// (set) Token: 0x0600157F RID: 5503 RVA: 0x00010817 File Offset: 0x0000EA17
		public int RecipeIndex { get; private set; }

		// Token: 0x06001580 RID: 5504 RVA: 0x00010820 File Offset: 0x0000EA20
		public ModRecipe(Mod mod)
		{
			this.mod = mod;
		}

		// Token: 0x06001581 RID: 5505 RVA: 0x0001082F File Offset: 0x0000EA2F
		public void SetResult(int itemID, int stack = 1)
		{
			this.createItem.SetDefaults(itemID, false);
			this.createItem.stack = stack;
		}

		// Token: 0x06001582 RID: 5506 RVA: 0x004140D0 File Offset: 0x004122D0
		public void SetResult(Mod mod, string itemName, int stack = 1)
		{
			if (mod == null)
			{
				mod = this.mod;
			}
			int num = mod.ItemType(itemName);
			if (num == 0)
			{
				string text = string.Concat(new string[]
				{
					"The item ",
					itemName,
					" does not exist in the mod ",
					mod.Name,
					".",
					Environment.NewLine
				});
				text += "If you are trying to use a vanilla item, try removing the first argument.";
				throw new RecipeException(text);
			}
			this.SetResult(num, stack);
		}

		// Token: 0x06001583 RID: 5507 RVA: 0x0001084A File Offset: 0x0000EA4A
		public void SetResult(ModItem item, int stack = 1)
		{
			this.SetResult(item.item.type, stack);
		}

		// Token: 0x06001584 RID: 5508 RVA: 0x0001085E File Offset: 0x0000EA5E
		public void AddIngredient(int itemID, int stack = 1)
		{
			this.requiredItem[this.numIngredients].SetDefaults(itemID, false);
			this.requiredItem[this.numIngredients].stack = stack;
			this.numIngredients++;
		}

		// Token: 0x06001585 RID: 5509 RVA: 0x00414148 File Offset: 0x00412348
		public void AddIngredient(Mod mod, string itemName, int stack = 1)
		{
			if (mod == null)
			{
				mod = this.mod;
			}
			int num = mod.ItemType(itemName);
			if (num == 0)
			{
				string text = string.Concat(new string[]
				{
					"The item ",
					itemName,
					" does not exist in the mod ",
					mod.Name,
					".",
					Environment.NewLine
				});
				text += "If you are trying to use a vanilla item, try removing the first argument.";
				throw new RecipeException(text);
			}
			this.AddIngredient(num, stack);
		}

		// Token: 0x06001586 RID: 5510 RVA: 0x00010895 File Offset: 0x0000EA95
		public void AddIngredient(ModItem item, int stack = 1)
		{
			this.AddIngredient(item.item.type, stack);
		}

		// Token: 0x06001587 RID: 5511 RVA: 0x004141C0 File Offset: 0x004123C0
		public void AddRecipeGroup(string name, int stack = 1)
		{
			if (!RecipeGroup.recipeGroupIDs.ContainsKey(name))
			{
				throw new RecipeException("A recipe group with the name " + name + " does not exist.");
			}
			int num = RecipeGroup.recipeGroupIDs[name];
			RecipeGroup recipeGroup = RecipeGroup.recipeGroups[num];
			this.AddIngredient(recipeGroup.ValidItems[recipeGroup.IconicItemIndex], stack);
			this.acceptedGroups.Add(num);
		}

		// Token: 0x06001588 RID: 5512 RVA: 0x000108A9 File Offset: 0x0000EAA9
		public void AddTile(int tileID)
		{
			if (tileID < 0 || tileID >= TileLoader.TileCount)
			{
				throw new RecipeException("No tile has ID " + tileID);
			}
			this.requiredTile[this.numTiles] = tileID;
			this.numTiles++;
		}

		// Token: 0x06001589 RID: 5513 RVA: 0x0041422C File Offset: 0x0041242C
		public void AddTile(Mod mod, string tileName)
		{
			if (mod == null)
			{
				mod = this.mod;
			}
			int num = mod.TileType(tileName);
			if (num == 0)
			{
				string text = string.Concat(new string[]
				{
					"The tile ",
					tileName,
					" does not exist in the mod ",
					mod.Name,
					".",
					Environment.NewLine
				});
				text += "If you are trying to use a vanilla tile, try using ModRecipe.AddTile(tileID).";
				throw new RecipeException(text);
			}
			this.AddTile(num);
		}

		// Token: 0x0600158A RID: 5514 RVA: 0x000108E9 File Offset: 0x0000EAE9
		public void AddTile(ModTile tile)
		{
			this.AddTile((int)tile.Type);
		}

		// Token: 0x0600158B RID: 5515 RVA: 0x00008CF7 File Offset: 0x00006EF7
		public virtual bool RecipeAvailable()
		{
			return true;
		}

		// Token: 0x0600158C RID: 5516 RVA: 0x00006B58 File Offset: 0x00004D58
		public virtual void OnCraft(Item item)
		{
		}

		// Token: 0x0600158D RID: 5517 RVA: 0x000108F7 File Offset: 0x0000EAF7
		public virtual int ConsumeItem(int type, int numRequired)
		{
			return numRequired;
		}

		// Token: 0x0600158E RID: 5518 RVA: 0x004142A4 File Offset: 0x004124A4
		public void AddRecipe()
		{
			if (this.createItem == null || this.createItem.type == 0)
			{
				throw new RecipeException("A recipe without any result has been added.");
			}
			if (this.numIngredients > 14 || this.numTiles > 14)
			{
				throw new RecipeException("A recipe with either too many tiles or too many ingredients has been added. 14 is the max.");
			}
			for (int i = 0; i < Recipe.maxRequirements; i++)
			{
				if (this.requiredTile[i] == 13)
				{
					this.alchemy = true;
					break;
				}
			}
			if (Recipe.numRecipes >= Recipe.maxRecipes)
			{
				Recipe.maxRecipes += 500;
				Array.Resize<Recipe>(ref Main.recipe, Recipe.maxRecipes);
				Array.Resize<int>(ref Main.availableRecipe, Recipe.maxRecipes);
				Array.Resize<float>(ref Main.availableRecipeY, Recipe.maxRecipes);
				for (int j = Recipe.numRecipes; j < Recipe.maxRecipes; j++)
				{
					Main.recipe[j] = new Recipe();
					Main.availableRecipeY[j] = 65f * (float)j;
				}
			}
			Main.recipe[Recipe.numRecipes] = this;
			this.RecipeIndex = Recipe.numRecipes;
			this.mod.recipes.Add(this);
			Recipe.numRecipes++;
		}

		// Token: 0x040014F7 RID: 5367
		public readonly Mod mod;

		// Token: 0x040014F8 RID: 5368
		private int numIngredients;

		// Token: 0x040014F9 RID: 5369
		private int numTiles;
	}
}
